package com.inetools.gwt.gajaxs.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
import com.inetools.gwetons.client.AbstractNativeJsData;

public class PicasaAlbumsProfilerJsBridge {
	public static class PicasaAlbumsProfilerNativeInputData 
	extends AbstractNativeJsData implements PicasaAlbumProfilerInputService {
		protected PicasaAlbumsProfilerNativeInputData() {
			super();
		}
		@Override
		public final String getPicasaUrl() {
			return getStringStrictImpl( "picasaUrl");
		}
		@Override
		public final int getCallbackMaxCount() {
			Integer callbackMaxCount = getIntImpl( "callbackMaxCount");
			if ( callbackMaxCount == null) return PicasaAlbumsProfiler.DEFAULT_CALLBACK_MAX_COUNT;
			return (callbackMaxCount > 0) ? callbackMaxCount : PicasaAlbumsProfiler.DEFAULT_CALLBACK_MAX_COUNT;
		}
		@Override
		public final int getCallbackRequestTimeOut() {
			Integer callbackRequestTimeOut = getIntImpl( "callbackRequestTimeOut");
			if ( callbackRequestTimeOut == null) return PicasaAlbumsProfiler.DEFAULT_CALLBACK_REQUEST_TIME_OUT;
			return (callbackRequestTimeOut > PicasaAlbumsProfiler.MIN_CALLBACK_REQUEST_TIME_OUT) ? callbackRequestTimeOut : PicasaAlbumsProfiler.MIN_CALLBACK_REQUEST_TIME_OUT;
		}
		@Override
		public final int getAlbumNumPerRow() {
			Integer albumNumPerRow = getIntImpl( "albumNumPerRow");
			if ( albumNumPerRow == null) return PicasaAlbumsProfiler.DEFAULT_ALBUM_NUM_PER_ROW;
			return ( albumNumPerRow > 0) ? albumNumPerRow : PicasaAlbumsProfiler.DEFAULT_ALBUM_NUM_PER_ROW;
		}
		@Override
		public final boolean isAlbumLocationOn() {
			Boolean albumLocationOn = getBooleanImpl( "albumLocationOn");
			return ( albumLocationOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_LOCATION_SWITCH : albumLocationOn;
		}
		@Override
		public final boolean isAlbumNameOn() {
			Boolean albumNameOn = getBooleanImpl( "albumNameOn");
			return ( albumNameOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_NAME_SWITCH : albumNameOn;
		}
		@Override
		public final boolean isAlbumPhotoCountsOn() {
			Boolean albumPhotoCountsOn = getBooleanImpl( "albumPhotoCountsOn");
			return ( albumPhotoCountsOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_PHOTO_COUNTS_SWITCH : albumPhotoCountsOn;
		}
		@Override
		public final boolean isAlbumThumbnailOn() {
			Boolean albumThumbnailOn = getBooleanImpl( "albumThumbnailOn");
			return ( albumThumbnailOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_THUMBNAIL_SWITCH : albumThumbnailOn;
		}
		@Override
		public final boolean isAlbumTitleOn() {
			Boolean albumTitleOn = getBooleanImpl( "albumTitleOn");
			return ( albumTitleOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_TITLE_SWITCH : albumTitleOn;
		}
		@Override
		public final boolean isAlbumUpdatedTimeOn() {
			Boolean albumUpdatedTimeOn = getBooleanImpl( "albumUpdatedTimeOn");
			return ( albumUpdatedTimeOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_UPDATED_TIME_SWITCH : albumUpdatedTimeOn;
		}
		@Override
		public final boolean isAlbumUserNameOn() {
			Boolean albumUserNameOn = getBooleanImpl( "albumUserNameOn");
			return ( albumUserNameOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_USER_NAME_SWITCH : albumUserNameOn;
		}
		@Override
		public final boolean isAlbumSummaryOn() {
			Boolean albumSummaryOn = getBooleanImpl( "albumSummaryOn");
			return ( albumSummaryOn == null) ? PicasaAlbumsProfiler.DEFAULT_ALBUM_SUMMARY_SWITCH : albumSummaryOn;
		}
		
		@Override
		public final String[] getExclusionIds() {
			JsArrayString exclusionIdArray = getJsStringArrayImp( "exclusionIds");
			if ( exclusionIdArray == null) return null;
			ArrayList<String> arrayList = new ArrayList<String>();
			for( int i = 0; i < exclusionIdArray.length(); i++) {
				arrayList.add( exclusionIdArray.get( i));
			} // for
			return arrayList.toArray( new String[]{});
		}
	} // protected class PicasaAlbumsProfilerNativeInputData extends JavaScriptObject
	
	protected PicasaAlbumsProfilerNativeInputData picasaAlbumsProfilerNativeInputData;	
	/**
	 * To find out whether picasaAlbumsProfilerNativeInputData is initialized
	 */
	protected boolean nativeInputDataInitilizationOn = false;
	public boolean isNativeInputDataInitilizationOn() {
		return nativeInputDataInitilizationOn;
	}
	
	// TODO To support multi instance, create wrapper class by extending Exportable interface, so this method on each instance can be used directly from javascript
	// About Exportable interface, refer to http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
	public final void setJsInputData( JavaScriptObject nativeInputData) {
		picasaAlbumsProfilerNativeInputData = 
			(PicasaAlbumsProfilerNativeInputData)nativeInputData;
		nativeInputDataInitilizationOn = true;
	}
	/**
	 * Expose setJsInputData method (which set picasaAlbumsProfilerNativeInputData field) to javascript
	 */
	protected native final void exposeSetJsInputData( PicasaAlbumsProfilerJsBridge thisInstance) /*-{
		$wnd.setPicasaAlbumsProfilerInputData = function( picasaAlbumsProfilerNativeInputData) {
			thisInstance.@com.inetools.gwt.gajaxs.client.PicasaAlbumsProfilerJsBridge::setJsInputData(Lcom/google/gwt/core/client/JavaScriptObject;)( picasaAlbumsProfilerNativeInputData);
		}
	}-*/;
	protected final native void setPicasaAlbumsProfilerData() /*-{
		if ( typeof( $wnd.initPicasaAlbumsProfiler) == "function") {
			$wnd.initPicasaAlbumsProfiler();
		}
	}-*/;
	public PicasaAlbumProfilerInputService getPicasaAlbumsProfilerInputData() throws RuntimeException {
		if ( !nativeInputDataInitilizationOn) {
			setPicasaAlbumsProfilerData();
			if ( !nativeInputDataInitilizationOn) {
				throw new RuntimeException( "Failed to read JavaScirpt input data.");
			}
		}
		return picasaAlbumsProfilerNativeInputData;
	}
	
	
	public PicasaAlbumsProfilerJsBridge() {
		exposeSetJsInputData( this);
	}
	
}
